﻿using Microsoft.Phone.Shell;
using System;
using System.Windows;
namespace Omnia.UX
{
    public class AppBarIconButton : AppBarMenuItem
    {
        public override IApplicationBarMenuItem ApplicationBarItem
        {
            get
            {
                if (_appBarItem == null)
                {
                    _appBarItem = new ApplicationBarIconButton();
                    if (!string.IsNullOrEmpty(IconUri))
                        (_appBarItem as ApplicationBarIconButton).IconUri = new Uri(IconUri, UriKind.RelativeOrAbsolute);
                    if (!string.IsNullOrEmpty(Text))
                        _appBarItem.Text = Text;
                    _appBarItem.Click += appBarItem_Click;
                }
                return _appBarItem;
            }
        }

        public virtual IApplicationBarIconButton ApplicationBarIconButton
        {
            get
            {
                return _appBarItem as IApplicationBarIconButton;
            }
        }

        #region - IconUri -
        public static readonly DependencyProperty IconUriProperty =
        DependencyProperty.Register("IconUri",
        typeof(string),
        typeof(AppBarIconButton),
        new PropertyMetadata("", new PropertyChangedCallback(AppBarIconButton.OnIconUriPropertyChanged)));

        public string IconUri
        {
            get
            {
                return (string)base.GetValue(IconUriProperty);
            }
            set
            {
                base.SetValue(IconUriProperty, value);
            }
        }

        private static void OnIconUriPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string url = e.NewValue as string;
            if (!string.IsNullOrEmpty(url) && (d as AppBarIconButton).ApplicationBarIconButton!=null)
                (d as AppBarIconButton).ApplicationBarIconButton.IconUri = new Uri(url, UriKind.RelativeOrAbsolute);
        }
        #endregion
    }
}
